package xin.pms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import xin.common.http.response.PageResponse;
import xin.common.http.response.Response;
import xin.pms.model.User;
import xin.pms.request.UserPageRequest;
import xin.pms.service.UserService;
import xin.pms.ui.AddUser;
import xin.pms.ui.UiUser;

/**
 * <pre>
 * User Controller
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 18:33
 */
@RestController
@RequestMapping("pms/user")
@Api(value = "xin-pms", description = "用户管理", position = 3)
@Slf4j
public class UserController {

    @Autowired
    private UserService service;


    @ApiOperation(value = "获取用户列表" , notes = "获取用户列表",response = PageResponse.class )
    @GetMapping("pageList")
    public PageResponse getUserList(UserPageRequest request){
        return service.findPageList(request);
    }

    @ApiOperation(value = "获取用户" , notes = "获取用户",response = Response.class )
    @GetMapping("{userId}")
    public Response get(@PathVariable String userId){
        Response<User> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.findOne(userId));
        return response;
    }

    @ApiOperation(value = "修改用户" , notes = "修改用户",response = Response.class )
    @PostMapping
    public Response post(@RequestBody UiUser user){
        Response response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(new UiUser(service.update(user)));
        return response;
    }

    @ApiOperation(value = "添加用户" , notes = "添加用户",response = Response.class )
    @PutMapping()
    public Response put(@RequestBody AddUser user){
        Response response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(new UiUser(service.add(user)));
        return response;
    }

    @ApiOperation(value = "删除用户" , notes = "删除用户",response = Response.class )
    @DeleteMapping("{userId}")
    public Response delete(@PathVariable String userId){
        Response response = new Response();
        response.setSuccess(Boolean.TRUE);
        service.delete(userId);
        return response;
    }
}
